package com.liuzu.esys.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liuzu.esys.bo.ItemBo;
import com.liuzu.esys.common.LayuiPageVo;
import com.liuzu.esys.mapper.ItemMapper;

import com.liuzu.esys.pojo.Item;
import com.liuzu.esys.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("itemService")
public class ItemServiceImpl implements ItemService {
    @Autowired
    private ItemMapper itemMapper;


    @Override
    public LayuiPageVo<Item> itemPageData(Map<String, Object> params) {
        //mybatis 的分页插件
        Integer page = Integer.parseInt(params.get("page") + "");

        Integer limit = Integer.parseInt(params.get("limit") + "");

        PageHelper.startPage(page, limit);

        List<Item> users = itemMapper.selectByParams(params);

        //mybatis 的分页数据结构
        PageInfo<Item> itemPageInfo = new PageInfo<>(users);

        //把mybatis 的分页数据封装到 layui 的分页数据结构中
        LayuiPageVo<Item> ItemsLayuiPageVo = new LayuiPageVo<>();
        ItemsLayuiPageVo .setCode(0); //layui 默认0 是正确状态
        ItemsLayuiPageVo .setMsg("分页列表数据");  //提示信息
        ItemsLayuiPageVo .setCount(itemPageInfo.getTotal());//总记录数
        ItemsLayuiPageVo .setData(itemPageInfo.getList());

        return ItemsLayuiPageVo;
    }

    @Override
    public int addItem(Item item) {
        int i = itemMapper.insertSelective(item);
        return i;
    }

    @Override
    public int deleteItem(Integer id) {
        return itemMapper.deleteKey(id) ;
    }

    @Override
    public int updateItem(Item item) {
        int i = itemMapper.updateItem(item);
        return i;
    }

    @Override
    public List<ItemBo> itemExcelData(Item item) {
        List<Item> items = itemMapper.selectBykey(item);
        List<ItemBo> itemBos = items.stream()
                .map((dr) -> {
                    ItemBo itemBo = new ItemBo();
                    itemBo .setId(dr.getId());
                    itemBo .setItem(dr.getItem());
                    itemBo .setPayment(dr.getPayment());
                    itemBo .setCharge(dr.getCharge());
                    itemBo .setTime(dr.getTime());
                    itemBo .setYear(dr.getYear());
                    itemBo .setPhone(dr.getPhone());
                    return itemBo;
                }).collect(Collectors.toList());

        return itemBos;
    }
}
